h(k) = k % 16
h(k) = ((k >> 4) ^ k) % 16
A good hash function spreads keys to avoid clustering, but collisions are still inevitable.
- Uniform Distribution: Each slot should be roughly equally likely.
- Fast & Deterministic: Cheap to compute, same input → same output.
- Inevitable Collisions: Finite buckets vs (often) huge key space.
Select an input sequence and insert keys.
The "Good Hash" uses bitwise mixing to decorrelate patterned inputs (e.g., multiples of 16). Bit mixing (shift + XOR) helps produce more varied lower bits before the modulo.
k >> 4: Pulls higher bits downward.^ (XOR): Combines patterns to break simple progressions.% 16: Final bucket selection.Inevitable Collisions & What To Do:
Takeaway: Distribute well, keep load reasonable, still handle collisions.